package com.mifengs.mall.util;

import android.net.Uri;
import android.text.TextUtils;

import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.MallApplication;

/**
 * Created by lihaoqi on 2018/1/15.
 */

public class WebUtils {

    public static String addSignParam(String urlString) {
//        UserInfo user = mAppContext.getUser();
//        if(user == null){
//            return urlString;
//        }
//        String userId = user.getUserId();
        String token = PreferenceUtils.getToken();
        if (!TextUtils.isEmpty(token)) {
            Uri uri = Uri.parse(urlString);
            String signValue = uri.getQueryParameter("app_token");
            if (signValue != null) {
                return urlString.replace(String.format("app_token=%s", signValue), String.format("app_token=%s", token));
            } else {
                String queryString = uri.getQuery();
                if (queryString != null && queryString.length() > 0)
                    return urlString = urlString + String.format("&app_token=%s", token);
                else
                    return urlString = urlString + String.format("?app_token=%s", token);
            }
        }
        return urlString;
    }

    public static String removeSignParam(String urlString) {
        Uri uri = Uri.parse(urlString);
        String signValue = uri.getQueryParameter("app_token");
        if (signValue != null) {
            String queryString = uri.getQuery();
            String[] params = queryString.split("&");
            if (params != null && params.length == 1)
                return urlString.replace(String.format("?app_token=%s", signValue), "");
            else
                return urlString.replace(String.format("app_token=%s", signValue), "");

        } else {
            return urlString;
        }
    }
}
